/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * panelCrearEmpleados.java
 *
 * Created on Apr 8, 2011, 6:28:47 PM
 */
package frontera.interfaz.panel;

import control.SIRHApp;
import control.comparaciones.ComparacionContenencia;
import control.comportamiento.EscucharBusqueda;
import control.filtros.FiltroCargosEstado;
import entidad.Bono;
import entidad.Cargo;
import entidad.Empleado;
import entidad.Falla;
import entidad.HoraExtra;
import entidad.Usuario;
import frontera.interfaz.panel.herramientas.PanelHerramientasBuscar;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *
 * @author tuareg
 */
public class PanelModificarEmpleado extends javax.swing.JPanel implements EscucharBusqueda<Cargo> {

    /** Creates new form panelCrearEmpleados */
    public PanelModificarEmpleado(Empleado modificarEmpleado) {

        initComponents();
        inicializarVariables(modificarEmpleado);
        inicializarPosibilidadesEstado();
        setFields();
        actualizarSalario();
    }

    private void inicializarVariables(Empleado modificarEmpleado) {
        Calendar cal = Calendar.getInstance();
        empleado = modificarEmpleado;
        empleadoTemporal = new Empleado("", "", 0, "", "", "", 0, null, "", cal);
        horasExtrasTemporales = new ArrayList<HoraExtra>();
        fallasTemporales = new ArrayList<Falla>();
        bonosTemporales = new ArrayList<Bono>();

        inicializarHorasExtrasTemporales();
        inicializarEmpleadoTemporal();
        inicializarBonosTemporales();
        inicializarFallasTemporales();
    }

    private void inicializarHorasExtrasTemporales() {
        List<HoraExtra> horasExtra = empleado.getHorasExtra();
        for (HoraExtra he : horasExtra) {
            horasExtrasTemporales.add(he);
        }
    }

    private void inicializarBonosTemporales() {
        List<Bono> bonos = empleado.getBonos();
        for (Bono bo : bonos) {
            bonosTemporales.add(bo);
        }
    }

    private void inicializarFallasTemporales() {
        List<Falla> fallas = empleado.getFallas();
        for (Falla fa : fallas) {
            fallasTemporales.add(fa);
        }
    }

    private void inicializarEmpleadoTemporal() {
        empleadoTemporal.setNombre(empleado.getNombre());
        empleadoTemporal.setCedula(empleado.getCedula());
        empleadoTemporal.setTelefono(empleado.getTelefono());
        empleadoTemporal.setDireccion(empleado.getDireccion());
        empleadoTemporal.setProfesion(empleado.getProfesion());
        empleadoTemporal.setEstado(empleado.getEstado());
        empleadoTemporal.setPorcentajeSalarioBas(empleado.getPorcentajeSalarioBas());
        empleadoTemporal.setCargoAsociado(empleado.getCargoAsociado());
        empleadoTemporal.setBonos(empleado.getBonos());
        empleadoTemporal.setEPS(empleado.getEPS());
        empleadoTemporal.setID(empleado.getID());
    }

    private void setFields() {
        labelEmpleado.setText(empleadoTemporal.getNombre());
        nombreTF.setText(empleadoTemporal.getNombre());
        fieldProfesion.setText(empleadoTemporal.getProfesion());
        fieldTelefono.setText(Long.toString(empleadoTemporal.getTelefono()));
        fieldDireccion.setText(empleadoTemporal.getDireccion());
        fieldCedula.setText(empleadoTemporal.getCedula());
        fieldPorcentaje.setText(Float.toString(empleadoTemporal.getPorcentajeSalarioBas()));

        if (empleadoTemporal.getCargoAsociado() != null) {
            fieldCargo.setText(empleadoTemporal.getCargoAsociado().getNombre());
        }

        fieldEPS.setText(empleadoTemporal.getEPS());
        boxEstado.setSelectedItem(empleadoTemporal.getEstado());
        }
        
    private void inicializarPosibilidadesEstado(){

        if ( !empleadoTemporal.getEstado().equals("Activo")){
            botonBonos.setEnabled(false);
            anadirFallaB.setEnabled(false);
            anadirHoraExtra.setEnabled(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        labelProfesion = new javax.swing.JLabel();
        fieldProfesion = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        fieldTelefono = new javax.swing.JTextField();
        labelTelefono = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        labelDireccion = new javax.swing.JLabel();
        fieldDireccion = new javax.swing.JTextField();
        jPanel10 = new javax.swing.JPanel();
        fieldCedula = new javax.swing.JTextField();
        labelCedula = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        boxEstado = new javax.swing.JComboBox();
        labelEstado = new javax.swing.JLabel();
        jPanel14 = new javax.swing.JPanel();
        jPanel18 = new javax.swing.JPanel();
        lebelPorcentaje = new javax.swing.JLabel();
        fieldPorcentaje = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        jPanel12 = new javax.swing.JPanel();
        jPanel15 = new javax.swing.JPanel();
        buscarCargoB = new javax.swing.JButton();
        fieldCargo = new javax.swing.JTextField();
        labelCargo = new javax.swing.JLabel();
        jPanel16 = new javax.swing.JPanel();
        labelValor = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel17 = new javax.swing.JPanel();
        jPanel19 = new javax.swing.JPanel();
        labelEPS = new javax.swing.JLabel();
        fieldEPS = new javax.swing.JTextField();
        jPanel20 = new javax.swing.JPanel();
        labelResultado = new javax.swing.JLabel();
        labelEmpleado = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        anadirFallaB = new javax.swing.JButton();
        anadirHoraExtra = new javax.swing.JButton();
        botonBonos = new javax.swing.JButton();
        guardarB = new javax.swing.JButton();
        cancelarB = new javax.swing.JButton();

        addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                formPropertyChange(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Dialog", 1, 32));
        jLabel7.setText("Editando Empleado");

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/UpdEmpleado.png"))); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Básicos"));
        jPanel1.setLayout(new java.awt.GridLayout(0, 1));

        jLabel1.setText("Nombres Y Apellidos");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1.add(jPanel4);

        jPanel5.setLayout(new java.awt.GridLayout(1, 0));

        labelProfesion.setText("Profesion");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelProfesion)
                .addGap(18, 18, 18)
                .addComponent(fieldProfesion, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelProfesion)
                    .addComponent(fieldProfesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel5.add(jPanel7);

        labelTelefono.setText("Teléfono");

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTelefono)
                .addGap(18, 18, 18)
                .addComponent(fieldTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTelefono))
                .addContainerGap())
        );

        jPanel5.add(jPanel8);

        jPanel1.add(jPanel5);

        jPanel6.setLayout(new java.awt.GridLayout(1, 0));

        labelDireccion.setText("Dirección");

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelDireccion)
                .addGap(20, 20, 20)
                .addComponent(fieldDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDireccion)
                    .addComponent(fieldDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel6.add(jPanel9);

        labelCedula.setText("Cédula");

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelCedula)
                .addGap(27, 27, 27)
                .addComponent(fieldCedula, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCedula))
                .addContainerGap())
        );

        jPanel6.add(jPanel10);

        jPanel1.add(jPanel6);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Laboral"));
        jPanel2.setLayout(new java.awt.GridLayout(0, 1));

        jPanel11.setLayout(new java.awt.GridLayout(1, 0));

        boxEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Activo", "Despedido", "Jubilado", "Vacaciones", "Incapacidad" }));
        boxEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxEstadoActionPerformed(evt);
            }
        });

        labelEstado.setText("Estado");

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boxEstado, 0, 252, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEstado)
                    .addComponent(boxEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel11.add(jPanel13);

        jPanel14.setLayout(new java.awt.GridLayout(1, 0));

        lebelPorcentaje.setText("Porcentaje Salarial");

        fieldPorcentaje.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                fieldPorcentajeKeyReleased(evt);
            }
        });

        jLabel11.setText("%");

        javax.swing.GroupLayout jPanel18Layout = new javax.swing.GroupLayout(jPanel18);
        jPanel18.setLayout(jPanel18Layout);
        jPanel18Layout.setHorizontalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel18Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lebelPorcentaje)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(fieldPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addGap(10, 10, 10))
        );
        jPanel18Layout.setVerticalGroup(
            jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel18Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lebelPorcentaje)
                    .addComponent(fieldPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel14.add(jPanel18);

        jPanel11.add(jPanel14);

        jPanel2.add(jPanel11);

        jPanel12.setLayout(new java.awt.GridLayout(1, 0));

        buscarCargoB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/search.png"))); // NOI18N
        buscarCargoB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarCargoBActionPerformed(evt);
            }
        });

        fieldCargo.setEditable(false);

        labelCargo.setText("Cargo");

        javax.swing.GroupLayout jPanel15Layout = new javax.swing.GroupLayout(jPanel15);
        jPanel15.setLayout(jPanel15Layout);
        jPanel15Layout.setHorizontalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel15Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelCargo)
                .addGap(23, 23, 23)
                .addComponent(fieldCargo, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buscarCargoB, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel15Layout.setVerticalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel15Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelCargo)
                        .addComponent(fieldCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(buscarCargoB, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel12.add(jPanel15);

        labelValor.setText("Valor");

        jLabel2.setText("Salario");

        javax.swing.GroupLayout jPanel16Layout = new javax.swing.GroupLayout(jPanel16);
        jPanel16.setLayout(jPanel16Layout);
        jPanel16Layout.setHorizontalGroup(
            jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel16Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(69, 69, 69)
                .addComponent(labelValor, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(139, Short.MAX_VALUE))
        );
        jPanel16Layout.setVerticalGroup(
            jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel16Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(labelValor))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel12.add(jPanel16);

        jPanel2.add(jPanel12);

        jPanel17.setLayout(new java.awt.GridLayout(1, 0));

        labelEPS.setText("EPS");

        javax.swing.GroupLayout jPanel19Layout = new javax.swing.GroupLayout(jPanel19);
        jPanel19.setLayout(jPanel19Layout);
        jPanel19Layout.setHorizontalGroup(
            jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel19Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelEPS, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(fieldEPS, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel19Layout.setVerticalGroup(
            jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel19Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel19Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldEPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelEPS))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel17.add(jPanel19);

        javax.swing.GroupLayout jPanel20Layout = new javax.swing.GroupLayout(jPanel20);
        jPanel20.setLayout(jPanel20Layout);
        jPanel20Layout.setHorizontalGroup(
            jPanel20Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 336, Short.MAX_VALUE)
        );
        jPanel20Layout.setVerticalGroup(
            jPanel20Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        jPanel17.add(jPanel20);

        jPanel2.add(jPanel17);

        labelResultado.setText(" ");

        labelEmpleado.setFont(new java.awt.Font("Dialog", 0, 22));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Añadir"));

        anadirFallaB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/fallaSmall.png"))); // NOI18N
        anadirFallaB.setText("Falla");
        anadirFallaB.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        anadirFallaB.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        anadirFallaB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                anadirFallaBActionPerformed(evt);
            }
        });

        anadirHoraExtra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/hExtraSmall.png"))); // NOI18N
        anadirHoraExtra.setText("Hora Extra");
        anadirHoraExtra.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        anadirHoraExtra.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        anadirHoraExtra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                anadirHoraExtraActionPerformed(evt);
            }
        });

        botonBonos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/bonosSmall.png"))); // NOI18N
        botonBonos.setText("Bonos");
        botonBonos.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonBonos.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botonBonos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBonosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(anadirFallaB, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(anadirHoraExtra)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonBonos, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(botonBonos, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(anadirFallaB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(anadirHoraExtra, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap())
        );

        guardarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Save.png"))); // NOI18N
        guardarB.setText("Guardar");
        guardarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarBActionPerformed(evt);
            }
        });

        cancelarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Cancelar.png"))); // NOI18N
        cancelarB.setText("Cancelar");
        cancelarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 683, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 406, Short.MAX_VALUE)
                        .addComponent(guardarB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelarB)
                        .addGap(23, 23, 23))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(labelEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 683, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelResultado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelarB)
                    .addComponent(guardarB))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void actualizarEmpleadoTemporal() {
        empleadoTemporal.setNombre(nombreTF.getText());
        empleadoTemporal.setCedula(fieldCedula.getText());
        empleadoTemporal.setTelefono(Long.parseLong(fieldTelefono.getText()));
        empleadoTemporal.setDireccion(fieldDireccion.getText());
        empleadoTemporal.setProfesion(fieldProfesion.getText());
        empleadoTemporal.setEstado((String) boxEstado.getSelectedItem());
        empleadoTemporal.setPorcentajeSalarioBas(Float.parseFloat(fieldPorcentaje.getText()));
        empleadoTemporal.setCargoAsociado(empleadoTemporal.getCargoAsociado());
        empleadoTemporal.setEPS(fieldEPS.getText());
        empleadoTemporal.setHorasExtra(horasExtrasTemporales);
        empleadoTemporal.setFallas(fallasTemporales);
        setFields();
    }

    private void anadirFallaBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_anadirFallaBActionPerformed
        actualizarEmpleadoTemporal();
        PanelFallasEmpleado fallas = new PanelFallasEmpleado(empleadoTemporal, fallasTemporales, this);
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(fallas);
    }//GEN-LAST:event_anadirFallaBActionPerformed

    private void buscarCargoBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarCargoBActionPerformed

        actualizarEmpleadoTemporal();
        ArrayList<Cargo> cargos = SIRHApp.getInstance().getAdmCargos().buscar(new FiltroCargosEstado(), new ComparacionContenencia(), "En Proceso");
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(SIRHApp.getInstance().getFactoryInterfaz().getPanelBusquedaCargo(this, this, this.getParent(),cargos));
    }//GEN-LAST:event_buscarCargoBActionPerformed

    private void anadirHoraExtraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_anadirHoraExtraActionPerformed
        actualizarEmpleadoTemporal();
        PanelHorasExtraEmpleado horasExtras = new PanelHorasExtraEmpleado(empleadoTemporal, horasExtrasTemporales, this);
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(horasExtras);

    }//GEN-LAST:event_anadirHoraExtraActionPerformed

    private void formPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_formPropertyChange
        setFields();
    }//GEN-LAST:event_formPropertyChange

    private void botonBonosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBonosActionPerformed
        actualizarEmpleadoTemporal();
        PanelBonosEmpleado bonos = new PanelBonosEmpleado(this, bonosTemporales,empleadoTemporal);
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(bonos);
    }//GEN-LAST:event_botonBonosActionPerformed

    private void boxEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxEstadoActionPerformed
        JComboBox cb = (JComboBox) evt.getSource();
        String str = String.valueOf(cb.getSelectedItem());
   
        if ( str.equals("Despedido") || str.equals("Jubilado")){
            boxEstado.setModel(new javax.swing.DefaultComboBoxModel(cambiarComboBox(str)));
        }

        if ( !str.equals("Activo") ){
            botonBonos.setEnabled(false);
            anadirFallaB.setEnabled(false);
            anadirHoraExtra.setEnabled(false);
        }
        
        else
        {
            botonBonos.setEnabled(true);
            anadirFallaB.setEnabled(true);
            anadirHoraExtra.setEnabled(true);
        }

        
    }//GEN-LAST:event_boxEstadoActionPerformed

    private void fieldPorcentajeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_fieldPorcentajeKeyReleased
        actualizarSalario();
    }//GEN-LAST:event_fieldPorcentajeKeyReleased

    private void guardarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarBActionPerformed

        String resultadoOperacion = SIRHApp.getInstance().getAdmEmpleados().actualizarEmpleado(empleado, 
                nombreTF.getText(),
                fieldCedula.getText(),
                fieldTelefono.getText(),
                fieldDireccion.getText(),
                fieldProfesion.getText(),
                (String) boxEstado.getSelectedItem(),
                fieldPorcentaje.getText(),
                empleadoTemporal.getCargoAsociado(),
                fieldEPS.getText(),
                horasExtrasTemporales,
                fallasTemporales,
                bonosTemporales);

        String estado = (String) boxEstado.getSelectedItem();
        if ( estado.equals("Despedido")){

            for (Usuario u: SIRHApp.getInstance().getSistema().getUsuarios()) {
                if (u.getEmpleadoAsociado().getNombre().equals(nombreTF.getText()) &&
                        !(SIRHApp.getInstance().getAdmPermisos().getUsuario() == u)){
                    SIRHApp.getInstance().getSistema().getUsuarios().remove(u);
                    break;
                }

                else if (SIRHApp.getInstance().getAdmPermisos().getUsuario() == u &&
                        u.getEmpleadoAsociado().getNombre().equals(nombreTF.getText())){
                    JOptionPane.showMessageDialog(this, "Este es el empleado que se encuentra actualmente asociado al usuario autenticado en el sistema. \n "
                            + "          " + "Su usuario no se podra eliminar hasta que se realice la tarea con un usuario distinto.","Error",JOptionPane.WARNING_MESSAGE);
                }

            }
        }

        if (resultadoOperacion.equals("")) {

            
            SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(
                    SIRHApp.getInstance().getVentanaPrincipal().getPanelEmpleados());
            ((PanelHerramientasBuscar<Empleado>) SIRHApp.getInstance().getFactoryInterfaz().getPanelEmpleados().getPanelHerramientas()).inicializarBusqueda();
            SIRHApp.getInstance().getVentanaPrincipal().getPanelEmpleados().actualizarLista(SIRHApp.getInstance().getSistema().getEmpleados());

        } else {
            labelResultado.setText(resultadoOperacion);
        }
}//GEN-LAST:event_guardarBActionPerformed

    private void cancelarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBActionPerformed
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(SIRHApp.getInstance().getVentanaPrincipal().getPanelEmpleados());
        ((PanelHerramientasBuscar<Empleado>) SIRHApp.getInstance().getFactoryInterfaz().getPanelEmpleados().getPanelHerramientas()).inicializarBusqueda();
        SIRHApp.getInstance().getVentanaPrincipal().getPanelEmpleados().actualizarLista(SIRHApp.getInstance().getSistema().getEmpleados());
}//GEN-LAST:event_cancelarBActionPerformed

    private void actualizarSalario(){

        if(empleadoTemporal.getCargoAsociado()!=null&&fieldPorcentaje.getText().equals("")!=true && (fieldPorcentaje.getText().matches("\\d*\\.\\d*") || fieldPorcentaje.getText().matches("\\d*"))){
            NumberFormat formatter = new DecimalFormat("#0.00");

            float porcentaje = Float.parseFloat(fieldPorcentaje.getText());
            float salario = porcentaje/100 * Float.parseFloat(empleadoTemporal.getCargoAsociado().getSalarioBásico());

            labelValor.setText(formatter.format(salario));
        }
        else{
            labelValor.setText(Float.toString(0));
        }
    }


    private String[] cambiarComboBox(String estadoActual){

        if( estadoActual.equals("Jubilado") || estadoActual.equals("Despedido") ){
            return new String[]{estadoActual, "Activo"};
        }

        return new String[] {};
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton anadirFallaB;
    private javax.swing.JButton anadirHoraExtra;
    private javax.swing.JButton botonBonos;
    private javax.swing.JComboBox boxEstado;
    private javax.swing.JButton buscarCargoB;
    private javax.swing.JButton cancelarB;
    private javax.swing.JTextField fieldCargo;
    private javax.swing.JTextField fieldCedula;
    private javax.swing.JTextField fieldDireccion;
    private javax.swing.JTextField fieldEPS;
    private javax.swing.JFormattedTextField fieldPorcentaje;
    private javax.swing.JTextField fieldProfesion;
    private javax.swing.JTextField fieldTelefono;
    private javax.swing.JButton guardarB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel20;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JLabel labelCargo;
    private javax.swing.JLabel labelCedula;
    private javax.swing.JLabel labelDireccion;
    private javax.swing.JLabel labelEPS;
    private javax.swing.JLabel labelEmpleado;
    private javax.swing.JLabel labelEstado;
    private javax.swing.JLabel labelProfesion;
    private javax.swing.JLabel labelResultado;
    private javax.swing.JLabel labelTelefono;
    private javax.swing.JLabel labelValor;
    private javax.swing.JLabel lebelPorcentaje;
    private javax.swing.JTextField nombreTF;
    // End of variables declaration//GEN-END:variables
    private Empleado empleado;
    private Empleado empleadoTemporal;
    private List<HoraExtra> horasExtrasTemporales;
    private List<Falla> fallasTemporales;
    private List<Bono> bonosTemporales;

    public void procesarResultadoBusqueda(Cargo resultado) {
        empleadoTemporal.setCargoAsociado(resultado);
        setFields();
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(this);
    }
}
